package com.csx.orm.mybatisplus.controller;

import com.csx.orm.mybatisplus.entity.User;
import com.csx.orm.mybatisplus.entity.dto.UserQueryCondition;
import com.csx.orm.mybatisplus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/mybatisplus")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getAllUsers")
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }

    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Long id) {
        return userService.getUserById(id);
    }

    @PostMapping("/getUserConditional")
    public List<User> getUserConditional(@RequestBody UserQueryCondition condition) {
        return userService.getUserConditional(condition);
    }

    @PostMapping("/createUser")
    public String createUser(@RequestBody User user) {
        userService.createUser(user);
        return "createUser Success";
    }

    @PostMapping("/updateUser/{id}")
    public String updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        userService.updateUser(user);
        return "updateUser Success";
    }

    @PostMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return "deleteUser Success";
    }

    @GetMapping("/countUser/{firstName}")
    public Integer countUser(@PathVariable String firstName) {
        return userService.countUser(firstName);
    }

    @GetMapping("/getUserListByCondition/{firstName}/{size}")
    public List<User> getUserListByCondition(@PathVariable String firstName, @PathVariable int size) {
        return userService.getUserListByCondition(firstName, size);
    }
}
